/*
 * File Name：ProgressCircle.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： ProgressCircle.java
 * Modify By：res-mingyang
 * Modify Date：2015-7-24
 * Modify Type：Add
 */
package com.ciwong.epaper.util.download;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.os.Looper;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 *
 */
public class ProgressCircleDownLoadState extends AppCompatImageView {
    private Context mContext;

    private int RADIU;

    private final float PERIMETER = 0.9f;

    private int mCurrentStatu;

    private Paint paint;

    private float progress, animationProgress;

    private int degree, degreeToGo;

    private final int NORMAL_COLOR = Color.parseColor("#97DE84");

    private final int ERROR_COLOR = Color.parseColor("#C8C8C8");

    private final int DOWNLOADING_COLOR = Color.parseColor("#19B300");

    private final int RED_COLOR = Color.parseColor("#FF0000");

    private final int GRAY_COLOR = Color.parseColor("#B0B1B0");

    private final int ARROW_COLOR = Color.parseColor("#d4d4d4");

    private int mCurrentColor;

    private RectF rectF;

    private Canvas canvas;

    private final float SPEED_FACTOR = 0.03f;

    private AnimationEndListener listener;

    private boolean isArrow = true;

    public ProgressCircleDownLoadState(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public ProgressCircleDownLoadState(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressCircleDownLoadState(Context context) {
        this(context, null);
    }

    private void init() {
        this.mCurrentStatu = DownloadStatus.STATUS_NORMAL;
        this.mCurrentColor = NORMAL_COLOR;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(mCurrentColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        animationProgress = 0f;
        this.canvas = canvas;
        paint.setColor(mCurrentColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setAlpha(255);
        int centerX = getMeasuredWidth() / 2;
        int centerY = getMeasuredHeight() / 2;
        if (RADIU == 0) {
            this.RADIU = (int) (getMeasuredHeight() * 0.4f);
        }
        super.onDraw(canvas);
        setRectF();
        if (!(mCurrentStatu == DownloadStatus.STATUS_LOADING
                || mCurrentStatu == DownloadStatus.STATUS_COMPLETED || mCurrentStatu == DownloadStatus.STATUS_NORMAL)) {
            if (mCurrentStatu == DownloadStatus.STATUS_FAILED || mCurrentStatu == DownloadStatus.STATUS_FAILED_SDISFULL
                    || mCurrentStatu == DownloadStatus.STATUS_FAILED_UNZIP) {
                paint.setColor(RED_COLOR);
            }

            if (mCurrentStatu == DownloadStatus.STATUS_FAILED || mCurrentStatu == DownloadStatus.STATUS_FAILED_SDISFULL
                    || mCurrentStatu == DownloadStatus.STATUS_FAILED_UNZIP
                    || mCurrentStatu == DownloadStatus.STATUS_DOWNLOADING
                    || mCurrentStatu == DownloadStatus.STATUS_PAUSE) {
                canvas.drawCircle(centerX, centerY, RADIU, paint);// 圆形
            } else {
                canvas.drawArc(rectF, 35, -360, false, paint);// 弧形
            }
        }

        if (mCurrentStatu == DownloadStatus.STATUS_DOWNLOADING) {
            drawDownloading(255, true);
            return;
        } else if (mCurrentStatu == DownloadStatus.STATUS_LOADING) {
            degree = degree + 5;
            canvas.rotate(degree, centerX, centerY);
            invalidate();
            canvas.drawArc(rectF, -90, 360 * PERIMETER, false, paint);
        }
        // if (gotoNormal() || gotoLoading() || gotoDownloading() || gotoPause()
        // || gotoFailed() || gotoCompleted())
        // {
        // return;
        // }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_NORMAL:
                drawNormal(centerX, centerY);
                break;
            case DownloadStatus.STATUS_COMPLETED:
                animationProgress = 1f;
                drawCompleted(centerX, centerY);
                break;
            case DownloadStatus.STATUS_PAUSE:
                drawPause(centerX, centerY, 255, true);
                break;
            case DownloadStatus.STATUS_FAILED:
            case DownloadStatus.STATUS_FAILED_SDISFULL:
            case DownloadStatus.STATUS_FAILED_UNZIP:
                drawFailed(centerX, centerY);
                break;
        }
    }

    private void drawNormal(int centerX, int centerY) {
//        if (animationProgress >= 0 && animationProgress <= 0.5f) {

        float stopVerticalY = centerY * 125 / 100;
        //中间竖线
        canvas.drawLine(centerX, centerY * 65 / 100, centerX, stopVerticalY, paint);
        // 下
        // canvas.drawLine(centerX * 3 / 4 + centerX * animationProgress /
        // 2,
        // centerY * 5 / 4 + centerY / 16,
        // centerX * 5 / 4 - centerX * animationProgress / 2,
        // centerY * 5 / 4 + centerY / 16, paint);
        float startY = centerY * 100 / 100;
        float startLeftX = centerX * 75 / 100;
        //箭头左边斜线
        canvas.drawLine(startLeftX, startY, centerX, stopVerticalY, paint);
        // 箭头右边斜线
        float startRightX = centerX + (centerX - startLeftX);
        canvas.drawLine(startRightX, startY, centerX, stopVerticalY, paint);
        //下方横线
        float startBottomLeftX = centerX * 58 / 100;
        float BottomY = centerY * 145 / 100;
        float stopBottomRightX = centerX + (centerX - startBottomLeftX);
        canvas.drawLine(startBottomLeftX, BottomY, stopBottomRightX, BottomY, paint);

        //下方左边斜线
        float startSlashLeftX = centerX * 48 / 100;
        float startSlashY = stopVerticalY;
        float stopSlashLeftX = startBottomLeftX;
        canvas.drawLine(startSlashLeftX, startSlashY, stopSlashLeftX, BottomY, paint);
        //下方右边斜线
        float startSlashRightX = centerX + (centerX - startSlashLeftX);
        float startSlashRightY = stopVerticalY;
        float stopSlashRightX = stopBottomRightX;
        canvas.drawLine(startSlashRightX, startSlashRightY, stopSlashRightX, BottomY, paint);

//        } else if (animationProgress > 0.5f && animationProgress <= 1) {
//            canvas.drawLine(centerX, centerY / 2, centerX,
//                    centerY * 5 / 4
//                            - centerY * 3 / 4 * (2 * animationProgress - 1f),
//                    paint);
//        }
    }

    private void drawDownloading(int alpha, boolean isDrawArc) {
        String text = String.valueOf(Math.round(progress * 100)) + "%";
        if (isDrawArc) {
            paint.setColor(DOWNLOADING_COLOR);
            canvas.drawArc(rectF, -90, 360 * progress, false, paint);
        }
        paint.setColor(GRAY_COLOR);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(getMeasuredWidth() * 0.2f);
        float tX = (getMeasuredWidth() - getFontlength(paint, text)) / 2;
        float tY = (getMeasuredHeight() - getFontHeight(paint)) / 2
                + getFontLeading(paint);
        paint.setAlpha(alpha);
        canvas.drawText(text, tX, tY, paint);
        paint.setColor(NORMAL_COLOR);
    }

    private void drawCompleted(int centerX, int centerY) {
        if (isArrow) {
            paint.setStrokeWidth(4);
            paint.setColor(ARROW_COLOR);
            canvas.drawLine(centerX - centerX / 5, centerY / 2 + centerY / 8,
                    centerX + centerX / 5, centerY, paint);
            canvas.drawLine(centerX - centerX / 5,
                    centerY * 3 / 2 - centerY / 8, centerX + centerX / 5,
                    centerY, paint);
        } else {
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(centerX, centerY, RADIU, paint);
            canvas.drawArc(rectF, -90, 360 * (1 - animationProgress), false,
                    paint);
            if (animationProgress >= 0 && animationProgress <= 0.3f) {
                canvas.drawLine(centerX - centerX / 4, centerY,
                        centerX - centerX / 4
                                + centerX * animationProgress / 0.3f / 4,
                        centerY + centerY * animationProgress / 0.3f / 4,
                        paint);
            } else if (animationProgress > 0.3f && animationProgress <= 1) {
                int x = centerX / 8;
                canvas.drawLine(centerX - centerX / 4 - x, centerY,
                        centerX - centerX / 4 + centerX / 4 - x,
                        centerY + centerY / 4, paint);
                canvas.drawLine(centerX - x, centerY + centerY / 4,
                        centerX + centerX * (animationProgress - 0.3f) / 2
                                - x / 2,
                        centerY + centerY / 4
                                - centerY * (animationProgress - 0.3f) * 3 / 4,
                        paint);
            }
        }
    }

    private void drawPause(int centerX, int centerY, int alpha,
                           boolean isDrawArc) {
        if (isDrawArc) {
            canvas.drawArc(rectF, -90, 360, false, paint);
        }
        paint.setStrokeWidth(3);
        paint.setAlpha(alpha);
        canvas.drawLine(centerX - centerX / 8, centerY - centerY / 5,
                centerX - centerX / 8, centerY + centerY / 5, paint);
        canvas.drawLine(centerX + centerX / 8, centerY - centerY / 5,
                centerX + centerX / 8, centerY + centerY / 5, paint);
    }

    private void drawFailed(int centerX, int centerY) {
        paint.setColor(RED_COLOR);
        // canvas.drawLine(centerX, centerY,
        // centerX - centerX / 4 * (1 - animationProgress),
        // centerY - centerY / 4 * (1 - animationProgress), paint);
        // canvas.drawLine(centerX, centerY,
        // centerX + centerX / 4 * (1 - animationProgress),
        // centerY - centerY / 4 * (1 - animationProgress), paint);
        // canvas.drawLine(centerX, centerY,
        // centerX - centerX / 4 * (1 - animationProgress),
        // centerY + centerY / 4 * (1 - animationProgress), paint);
        // canvas.drawLine(centerX, centerY,
        // centerX + centerX / 4 * (1 - animationProgress),
        // centerY + centerY / 4 * (1 - animationProgress), paint);
        // 小圆点
        canvas.drawCircle(centerX, centerY * 5 / 4, 2, paint);
        Paint pointPaint = paint;
        pointPaint.setStrokeWidth(5);
        // 中间竖线
        canvas.drawLine(centerX, centerY / 3 * 2, centerX, centerY * 10 / 9,
                pointPaint);
        // canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
        // R.mipmap.icon_submit_fail_tip), 0, 0, paint);
        paint.setColor(NORMAL_COLOR);
    }

    private void drawDownloadingAndPause(int centerX, int centerY) {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_DOWNLOADING_TO_PAUSE:
                if (animationProgress >= 0 && animationProgress <= 0.5f) {
                    drawDownloading((int) ((1 - animationProgress * 2) * 255),
                            false);
                } else if (animationProgress > 0.5f && animationProgress <= 1) {
                    drawPause(centerX, centerY,
                            (int) ((animationProgress - 0.5) * 2 * 255), false);
                }
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(3);
                paint.setAlpha(255);
                canvas.drawArc(rectF, -90,
                        360 * progress
                                + (1 - progress) * 360 * animationProgress,
                        false, paint);
                break;
            case DownloadStatus.STATUS_PAUSE_TO_DOWNLOADING:
                if (animationProgress >= 0 && animationProgress <= 0.5f) {
                    drawPause(centerX, centerY,
                            (int) ((1 - animationProgress * 2) * 255), false);
                } else if (animationProgress > 0.5f && animationProgress <= 1) {
                    drawDownloading((int) ((animationProgress - 0.5) * 2 * 255),
                            false);
                }
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(3);
                paint.setAlpha(255);
                canvas.drawArc(rectF, -90,
                        360 - (1 - progress) * 360 * animationProgress, false,
                        paint);
                break;
        }
    }

    private void drawCompletedToNormal(int centerX, int centerY) {
        // 左边斜线
        canvas.drawLine(centerX - centerX / 4, centerY,
                centerX - centerX / 4 + centerX / 4, centerY + centerY / 4,
                paint);
        // 下边横线
        // canvas.drawLine(centerX * 3 / 4 + centerX * animationProgress / 4,
        // centerY * 5 / 4,
        // centerX * 5 / 4 - centerX * animationProgress / 4,
        // centerY * 5 / 4, paint);
        // 中间竖线
        canvas.drawLine(centerX, centerY * 5 / 4,
                centerX + centerX * (1 - animationProgress) / 4,
                centerY + centerY / 4 * animationProgress, paint);
        // 右边斜线
        canvas.drawLine(centerX, centerY + centerY / 4,
                centerX + centerX * animationProgress / 2, centerY / 2, paint);
    }

    private void drawFailedToNormal(int centerX, int centerY) {
        // 下边横线
        // canvas.drawLine(centerX * 3 / 4, centerY * 5 / 4, centerX,
        // centerY * 5 / 4 - centerY / 4 * animationProgress, paint);
        // canvas.drawLine(centerX * 5 / 4, centerY * 5 / 4, centerX,
        // centerY * 5 / 4 - centerY / 4 * animationProgress, paint);
        // 左边斜线
        canvas.drawLine(centerX,
                centerY * 5 / 4 - centerY / 4 * animationProgress,
                centerX * 3 / 4, centerY - centerY / 4 * animationProgress,
                paint);
        // 右边斜线
        canvas.drawLine(centerX,
                centerY * 5 / 4 - centerY / 4 * animationProgress,
                centerX * 5 / 4, centerY - centerY / 4 * animationProgress,
                paint);
        // 中间竖线
        canvas.drawLine(centerX, centerY / 2 + centerY / 2 * animationProgress,
                centerX, centerY * 5 / 4 - centerY / 4 * animationProgress,
                paint);

    }

    private boolean gotoNormal() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_LOADING_TO_NORMAL:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_NORMAL;
                    return false;
                }
                canvas.drawArc(rectF, -90, 360 * PERIMETER * animationProgress,
                        false, paint);
                drawNormal(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_DOWNLOADING_TO_NORMAL:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_NORMAL;
                    return false;
                }
                canvas.drawArc(rectF, -90, 360 * progress * animationProgress,
                        false, paint);
                drawNormal(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_COMPLETED_TO_NORMAL:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_NORMAL;
                    return false;
                }
                drawCompletedToNormal(getMeasuredWidth() / 2,
                        getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_PAUSE_TO_NORMAL:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_NORMAL;
                    return false;
                }
                canvas.drawArc(rectF, -90, 360 * animationProgress, false,
                        paint);
                drawNormal(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_FAILED_TO_NORMAL:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_NORMAL;
                    return false;
                }
                drawFailedToNormal(getMeasuredWidth() / 2,
                        getMeasuredHeight() / 2);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    private boolean gotoLoading() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_NORMAL_TO_LOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_LOADING;
                    if (listener != null) {
                        listener.gotoLoadingEnd(DownloadStatus.STATUS_NORMAL);
                    }
                }
                canvas.drawArc(rectF, -90, 360 * PERIMETER * animationProgress,
                        false, paint);
                drawNormal(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_FAILED_TO_LOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_LOADING;
                    if (listener != null) {
                        listener.gotoLoadingEnd(DownloadStatus.STATUS_FAILED);
                    }
                }
                canvas.drawArc(rectF, -90, 360 * PERIMETER * animationProgress,
                        false, paint);
                drawFailed(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    private boolean gotoDownloading() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_NORMAL_TO_DOWNLOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
                    drawDownloading(255, true);
                    if (listener != null) {
                        listener.gotoDownloadingEnd(
                                DownloadStatus.STATUS_NORMAL);
                    }
                    return true;
                }
                drawNormal(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_LOADING_TO_DOWNLOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
                    drawDownloading(255, true);
                    if (listener != null) {
                        listener.gotoDownloadingEnd(
                                DownloadStatus.STATUS_LOADING);
                    }
                    return true;
                }
                canvas.rotate(degreeToGo * (1 - animationProgress),
                        getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                canvas.drawArc(rectF, -90,
                        360 * PERIMETER * (1 - animationProgress), false,
                        paint);
                break;
            case DownloadStatus.STATUS_PAUSE_TO_DOWNLOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
                    drawDownloading(255, true);
                    if (listener != null) {
                        listener.gotoDownloadingEnd(
                                DownloadStatus.STATUS_PAUSE);
                    }
                    return true;
                }
                drawDownloadingAndPause(getMeasuredWidth() / 2,
                        getMeasuredHeight() / 2);
                break;
            case DownloadStatus.STATUS_FAILED_TO_DOWNLOADING:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
                    drawDownloading(255, true);
                    if (listener != null) {
                        listener.gotoDownloadingEnd(
                                DownloadStatus.STATUS_FAILED);
                    }
                    return true;
                }
                canvas.drawArc(rectF, -90, 360 * progress * animationProgress,
                        false, paint);
                drawFailed(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    private boolean gotoCompleted() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_DOWNLOADING_TO_COMPLETED:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_COMPLETED;
                    return false;
                }
                drawCompleted(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    private boolean gotoPause() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_DOWNLOADING_TO_PAUSE:
                setRectF();
                animationProgress = animationProgress + SPEED_FACTOR;
                if (animationProgress >= 1f) {
                    animationProgress = 1f;
                    mCurrentStatu = DownloadStatus.STATUS_PAUSE;
                    return false;
                }
                drawDownloadingAndPause(getMeasuredWidth() / 2,
                        getMeasuredHeight() / 2);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    private boolean gotoFailed() {
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_LOADING_TO_FFILED:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_FAILED;
                    return false;
                }
                drawFailed(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                canvas.drawArc(rectF, -90, 360 * PERIMETER * animationProgress,
                        false, paint);
                break;
            case DownloadStatus.STATUS_DOWNLOADING_TO_FAILED:
                setRectF();
                animationProgress = animationProgress - SPEED_FACTOR;
                if (animationProgress <= 0f) {
                    animationProgress = 0f;
                    mCurrentStatu = DownloadStatus.STATUS_FAILED;
                    return false;
                }
                drawFailed(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                canvas.drawArc(rectF, -90, 360 * progress * animationProgress,
                        false, paint);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    public void updateDownloadProgress(float progress) {
        this.progress = progress;
        this.mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
        invalidate();
    }

    // @Override
    // public boolean onTouchEvent(MotionEvent event)
    // {
    // if (mCurrentStatu == DownloadStatus.STATUS_COMPLETED)
    // {
    // return super.onTouchEvent(event);
    // }
    // switch (event.getAction())
    // {
    // case MotionEvent.ACTION_DOWN:
    // setAlpha(0.5f);
    // break;
    // case MotionEvent.ACTION_UP:
    // setAlpha(1f);
    // if (isPressed())
    // {
    // performClick();
    // }
    // break;
    // case MotionEvent.ACTION_MOVE:
    // float viewX = event.getX();
    // float viewY = event.getY();
    // if (viewX < 0 || viewY < 0 || viewX > getWidth()
    // || viewY > getHeight())
    // {
    // setAlpha(1f);
    // }
    // else if (viewX > 0 || viewY > 0 || viewX < getWidth()
    // || viewY < getHeight())
    // {
    // setAlpha(0.5f);
    // setPressed(true);
    // }
    // break;
    // }
    // return super.onTouchEvent(event);
    // }

    public void setIsError(boolean isError) {
        this.mCurrentColor = isError ? ERROR_COLOR : NORMAL_COLOR;
    }

    public void initToNormal() {
        this.mCurrentStatu = DownloadStatus.STATUS_NORMAL;
        invalidate();
    }

    public void initToLoading() {
        progress = 0;
        this.mCurrentStatu = DownloadStatus.STATUS_LOADING;
        invalidate();
    }

    public void initToDownloading() {
        this.mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING;
        invalidate();
    }

    public void initToCompleted() {
        this.mCurrentStatu = DownloadStatus.STATUS_COMPLETED;
        invalidate();
    }

    public void initToPause() {
        this.mCurrentStatu = DownloadStatus.STATUS_PAUSE;
        invalidate();
    }

    public void initToFAILED() {
        this.mCurrentStatu = DownloadStatus.STATUS_FAILED;
        invalidate();
    }

    public void setToNormalStatu() {
        if (canvas == null) {
            return;
        }
        animationProgress = 1f;
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_LOADING:
                mCurrentStatu = DownloadStatus.STATUS_LOADING_TO_NORMAL;
                break;
            case DownloadStatus.STATUS_DOWNLOADING:
                mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING_TO_NORMAL;
                break;
            case DownloadStatus.STATUS_COMPLETED:
                mCurrentStatu = DownloadStatus.STATUS_COMPLETED_TO_NORMAL;
                break;
            case DownloadStatus.STATUS_PAUSE:
                mCurrentStatu = DownloadStatus.STATUS_PAUSE_TO_NORMAL;
                break;
            case DownloadStatus.STATUS_FAILED:
            case DownloadStatus.STATUS_FAILED_SDISFULL:
            case DownloadStatus.STATUS_FAILED_UNZIP:
                mCurrentStatu = DownloadStatus.STATUS_FAILED_TO_NORMAL;
                break;
            default:
                return;
        }
        gotoNormal();
    }

    public void setToLoadingStatu() {
        if (canvas == null) {
            return;
        }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_NORMAL:
                degree = 0;
                mCurrentStatu = DownloadStatus.STATUS_NORMAL_TO_LOADING;
                break;
            case DownloadStatus.STATUS_FAILED:
            case DownloadStatus.STATUS_FAILED_SDISFULL:
            case DownloadStatus.STATUS_FAILED_UNZIP:
                degree = 0;
                mCurrentStatu = DownloadStatus.STATUS_FAILED_TO_LOADING;
                break;
            default:
                return;
        }
        gotoLoading();
    }

    public void setToDownloadingStatu() {
        if (canvas == null) {
            return;
        }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_NORMAL:
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_NORMAL_TO_DOWNLOADING;
                break;
            case DownloadStatus.STATUS_LOADING:
                degreeToGo = degree % 360 + 90;
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_LOADING_TO_DOWNLOADING;
                break;
            case DownloadStatus.STATUS_PAUSE:
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_PAUSE_TO_DOWNLOADING;
                break;
            case DownloadStatus.STATUS_FAILED:
            case DownloadStatus.STATUS_FAILED_SDISFULL:
            case DownloadStatus.STATUS_FAILED_UNZIP:
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_FAILED_TO_DOWNLOADING;
                break;
            default:
                return;
        }
        gotoDownloading();
    }

    public void setToCompleted() {
        if (canvas == null) {
            return;
        }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_DOWNLOADING:
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING_TO_COMPLETED;
                break;
            default:
                return;
        }
        gotoCompleted();
    }

    public void setToPauseStatu() {
        if (canvas == null) {
            return;
        }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_DOWNLOADING:
                animationProgress = 0f;
                mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING_TO_PAUSE;
                break;
            default:
                return;
        }
        gotoPause();
    }

    public void setToFailedStatu() {
        if (canvas == null) {
            return;
        }
        switch (mCurrentStatu) {
            case DownloadStatus.STATUS_LOADING:
                mCurrentStatu = DownloadStatus.STATUS_LOADING_TO_FFILED;
                animationProgress = 1f;
                break;
            case DownloadStatus.STATUS_DOWNLOADING:
                mCurrentStatu = DownloadStatus.STATUS_DOWNLOADING_TO_FAILED;
                animationProgress = 1f;
                break;
            default:
                return;
        }
        gotoFailed();
    }

    /**
     * 更新ui
     */
    @Override
    public void invalidate() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            super.invalidate();
        } else {
            super.postInvalidate();
        }
    }

    private void setRectF() {
        if (rectF == null) {
            rectF = new RectF();
            rectF.left = getMeasuredWidth() / 2 - RADIU;
            rectF.top = getMeasuredHeight() / 2 - RADIU;
            rectF.right = getMeasuredWidth() / 2 + RADIU;
            rectF.bottom = getMeasuredHeight() / 2 + RADIU;
        }
    }

    /**
     * @return 返回指定笔和指定字符串的长度
     */
    public float getFontlength(Paint paint, String str) {
        return paint.measureText(str);
    }

    /**
     * @return 返回指定笔的文字高度
     */
    public float getFontHeight(Paint paint) {
        FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }

    /**
     * @return 返回指定笔离文字顶部的基准距离
     */
    public float getFontLeading(Paint paint) {
        FontMetrics fm = paint.getFontMetrics();
        return fm.leading - fm.ascent;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 sp
     */
    public static int px2sp(Context context, float pxValue) {
        final float scale = context.getResources()
                .getDisplayMetrics().scaledDensity;
        return (int) (pxValue / scale);
    }

    // public void setPressedColor()
    // {
    // this.mCurrentColor = PRESSED_COLOR;
    // invalidate();
    // }
    //
    // public void setNormalColor()
    // {
    // this.mCurrentColor = NORMAL_COLOR;
    // invalidate();
    // }

    public int getCurrentStatu() {
        return this.mCurrentStatu;
    }

    public void setAnimationEndListener(AnimationEndListener listener) {
        this.listener = listener;
    }

    interface AnimationEndListener {
        void gotoLoadingEnd(int statu);

        void gotoDownloadingEnd(int statu);
    }

    public void setIsArrow(boolean isArrow) {
        this.isArrow = isArrow;
    }

}
